var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http=require('https');
var ejs = require('ejs');
var session=require('express-session');

var routes = require('./routes/index');
var users = require('./routes/users');
var login=require('./routes/login');
var register=require('./routes/register');


var app = express();

// view engine setup
//设定端口
app.set('port',process.env.PORT||8080);
//设定视图路径
app.set('views', path.join(__dirname, 'views'));
//设定视图引擎模板，还可以设定其他模板，后续介绍
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser("An"));
app.use(session({
    secret:'an',
    resave:false,
    saveUninitialized:true
}))
//设定静态文件路径
app.use(express.static(path.join(__dirname, 'public')));

//若get请求的url为当前路径，则交给路由index入口中的对应函数处理
app.use('/', routes);//首页
app.use('/users', users);
app.use('/login',login);
app.use('/register',register);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
http.createServer(app).listen(app.get('port'),function () {
    console.log('Express server listening on port ' + app.get('port'));
})


module.exports = app;
